{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "79240511",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "# 从 CSV 文件读取数据\n",
    "data_raw = pd.read_csv('raw.csv')\n",
    "data = data_raw.to_numpy()\n",
    "\n",
    "result_list = []\n",
    "for row in data:\n",
    "    row_list = row[0].split(';')  # 用分号分割字符串\n",
    "    # 移除第一个、第三个和第四个元素\n",
    "    filtered_row = [row_list[i] for i in range(len(row_list)) if i not in [0, 2, 3]]\n",
    "    # 将每个元素转换为浮点数（如果可能的话）\n",
    "    float_row = []\n",
    "    for item in filtered_row:\n",
    "        try:\n",
    "            float_row.append(float(item))\n",
    "        except ValueError:\n",
    "            float_row.append(item)  # 如果无法转换为浮点数，则保持原样\n",
    "    result_list.append(float_row)\n",
    "    \n",
    "    \n",
    "# 去掉每个维度的最后一个元素\n",
    "data = [sublist[:-1] for sublist in result_list]\n",
    "\n",
    "\n",
    "# 将二维列表转换为三维列表\n",
    "group_size = 6  # 每个子列表的大小\n",
    "data_3 = [\n",
    "    data[i:i + group_size] for i in range(0, len(data), group_size)\n",
    "]\n",
    "\n",
    "#数据整合\n",
    "data_four = [\n",
    "    [[data[i][0]]] + [data[i][j:j+6] for j in range(1, len(data[i]), 6)]\n",
    "    for i in range(len(data))\n",
    "]\n",
    "\n",
    "\n",
    "data_1 = []\n",
    "data_2 = []\n",
    "\n",
    "for sublist in data_four:\n",
    "    if len(sublist) > 751:\n",
    "        data_1.append([sublist[0]] + sublist[1:377])\n",
    "        data_2.append([sublist[0]] + sublist[377:752])\n",
    "    else:\n",
    "        pass  # 如果不满足条件，直接跳过\n",
    "\n",
    "    \n",
    "import csv\n",
    "# 将数据写入CSV文件\n",
    "with open('input_output.csv', 'w', newline='') as csvfile:\n",
    "    fieldnames = ['input', 'output']\n",
    "    csvwriter = csv.DictWriter(csvfile, fieldnames=fieldnames)\n",
    "    \n",
    "    # 写入列名\n",
    "    csvwriter.writeheader()\n",
    "    \n",
    "    # 同时处理data和data_2列表\n",
    "    for sublist1, sublist2 in zip(data_1, data_2):\n",
    "        # 将子列表中的内容连接成特定格式的字符串\n",
    "        formatted_data_1 = ';'.join([','.join(map(str, item)) if isinstance(item, list) else str(item) for item in sublist1])\n",
    "        formatted_data_2 = ';'.join([','.join(map(str, item)) if isinstance(item, list) else str(item) for item in sublist2])\n",
    "        \n",
    "        # 写入CSV文件的一行\n",
    "        csvwriter.writerow({'input': formatted_data_1, 'output': formatted_data_2})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "cac1934a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[' Motorcycle']"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#每个data[i][0]都存储的是类别，是一个字符串，比如‘Motorcycle’,'Taxi'\n",
    "data_1[0][0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "f07dba39",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[37.98009, 23.735504, 29.0144, -0.0072, -0.2004, 0.0]"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_1[0][1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "792ecf5c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[37.977719, 23.737482, 38.4169, -0.8233, -0.361, 30.0]"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_2[0][-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "49128e8d",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
